package com.inspection.controller;

import com.inspection.entity.InspectionRegulationDO;
import com.inspection.model.dto.InspectionRegulationDTO;
import com.inspection.model.dto.InspectionRegulationQueryDTO;
import com.inspection.model.vo.InspectionTypeVO;
import com.inspection.service.InspectionRegulationService;
import com.inspection.support.AssertUtils;
import com.inspection.support.Wrapper;
import com.inspection.support.anno.SysLogAnno;
import com.inspection.support.model.DataTree;
import com.inspection.support.model.ItemList;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class InspectionRegulationController {

    @Autowired
    private InspectionRegulationService inspectionRegulationService;

    /**
     * 支持列表查询
     * 也可以支持部门id 来匹配条例
     * @param page
     * @return
     */
    @ApiOperation("获取考核条例分页")
    @RequestMapping("/inspect/regulation/page")
    @Deprecated
    public Wrapper<Page<InspectionRegulationDO>> pageRegulation(@RequestBody InspectionRegulationQueryDTO page){
        return Wrapper.ok(inspectionRegulationService.pageRegulation(page));
    }

    /**
     * 支持列表查询
     * 也可以支持部门id 来匹配条例 【可执行的条例】
     *
     * @param query
     * @return
     */
    @ApiOperation("获取可执行的考核条例列表")
    @RequestMapping("/inspect/regulation/query")
    public Wrapper<ItemList<InspectionRegulationDO>> queryRegulation(@RequestBody InspectionRegulationQueryDTO query) {
        return Wrapper.ok(ItemList.buildItems(inspectionRegulationService.queryRegulation(query)));
    }


    @SysLogAnno(operateCode =110601,operateName = "考核条例：保存")
    @ApiOperation("保存考核条例")
    @RequestMapping("/inspect/regulation/save")
    public Wrapper<String> saveRegulation(@RequestBody InspectionRegulationDTO save){
        AssertUtils.nonNull(save.getIsLeafNode(),"条例类型不能为空");
        AssertUtils.nonNull(save.getParentId(),"父节点id不能为空");
        AssertUtils.nonNull(save.getRegulationNo(),"条例编号不能为空");
        inspectionRegulationService.saveRegulation(save);
        return  Wrapper.ok("ok");
    }

    @ApiOperation("获取考核条例")
    @RequestMapping("/inspect/regulation/get")
    public Wrapper<InspectionRegulationDO> getRegulation(@RequestBody InspectionRegulationDTO query){
       return Wrapper.ok(inspectionRegulationService.getRegulation(query));
    }

    @SysLogAnno(operateCode =110602,operateName = "考核条例：更新")
    @ApiOperation("编辑考核条例")
    @RequestMapping("/inspect/regulation/update")
    public Wrapper<String> updateRegulation(@RequestBody InspectionRegulationDTO update){
        inspectionRegulationService.updateRegulation(update);
        return  Wrapper.ok("ok");
    }

    @SysLogAnno(operateCode =110603,operateName = "考核条例：删除")
    @ApiOperation("刪除考核条例")
    @RequestMapping("/inspect/regulation/del")
    public Wrapper<String> delRegulation(@RequestBody InspectionRegulationDTO del){
        inspectionRegulationService.delRegulation(del);
        return  Wrapper.ok("ok");
    }



    @ApiOperation("获取考核条例的tree列表")
    @RequestMapping("/inspect/regulation/tree")
    public Wrapper<DataTree> getRegulationTree(@RequestBody InspectionRegulationQueryDTO query) {

        return Wrapper.ok((inspectionRegulationService.getRegulationTree(query)));
    }

}
